<?php

/**
 * This is the model class for table "pdf".
 *
 * The followings are the available columns in table 'pdf':
 * @property integer $id
 * @property integer $id_location
 * @property string $path
 */
class Pdf extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Pdf the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'pdf';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id_location, path', 'required'),
			array('id_location', 'numerical', 'integerOnly'=>true),
			array('path', 'length', 'max'=>250),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, id_location, path', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		'idLocation' => array(self::BELONGS_TO, 'Location', 'id_location'),				
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'id_location' => 'Id Location',
			'path' => 'Path',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('id_location',$this->id_location);
		$criteria->compare('path',$this->path,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * Method to get the pdf path
	 * @return $string pdf path
	 */
	public function getFullPath(){
		return Yii::app()->baseUrl.'/images/locations/'.$this->id_location.'/pdf/'.$this->path;
	}
	
	/**
	 * Method to delete a pdf file
	 * @param integer $id pdf id
	 * @return integer $id_location location id
	 */
	public static function deletePdf($id)
	{
		// Delete pdf file
		$pdf=Pdf::model()->findBypk($id);
	
		$folder='../'.Yii::app()->baseUrl.'/images/locations/'.$pdf->id_location.'/pdf';
		$file=$folder.'/'.$pdf->path;
	
		try	{
			unlink($file);
		}
		catch (Exception $e) {
			echo "Erreur : ".$e;
		}
		return $pdf->id_location;
	}
	
	/* // upload d'une image dans son repertoire & insertion sql
	 */
	public static function uploadPdf($model)
	{
		if ($_FILES['Location']['name']['pdf']!="")
		{
			try {
					
				$info=pathinfo($_FILES['Location']['name']['pdf']);
				$name=$_FILES['Location']['name']['pdf'];
					
				$pdf=new Pdf();
				$pdf->path=$name;
				$pdf->id_location=$model->id;
				$pdf->save();
	
				$folder='images/locations/'.$model->id.'/';
				if(!is_dir($folder)){
					mkdir ($folder, 0700);
				}
	
					
				$folder_pdf='images/locations/'.$model->id.'/pdf/';
					
				if(!is_dir($folder_pdf)){
					mkdir ($folder_pdf, 0700);
				}
	
				CUploadedFile::getInstance($model,'pdf')->saveAs($folder_pdf.$name);
				return $name;
	
	
			} catch (Exception $e) {
				echo "Erreur : ".$e;
				return null;
			}
		}
	}	
}